package com.album.activity.adapter;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;

import com.album.R;
import com.album.common.Constants;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {  
	private Context context;
//	private List<SoftReference<Bitmap>> listBitmap = new ArrayList<SoftReference<Bitmap>>();
	private List<Bitmap> listBitmap = new ArrayList<Bitmap>();

	public ImageAdapter(Context context, List<Bitmap> listbitmap) {
		this.context = context;
		this.listBitmap = listbitmap;
	}
		public View getView(int position, View convertView, ViewGroup parent)     
	    {     
	      ImageView imageView = new ImageView(context);      
	      Bitmap bitmap = listBitmap.get(position);
	      if(bitmap!=null){
	    	  imageView.setImageBitmap(bitmap);  
	      }
	     
	      imageView.setLayoutParams(new GridView.LayoutParams(Constants.m_screenWidth,Constants.m_screenWidth));//设置ImageView宽高 
          imageView.setAdjustViewBounds(false); 
          imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
          imageView.setPadding(4, 4, 4, 4);
	        return imageView;     
	    }   
//	public View getView(int position, View convertView, ViewGroup parent) { 
//        ImageView imageView; 
//        if (convertView == null) { 
//            imageView = new ImageView(context); 
//            System.out.println("position:"+position+" "+Constants.m_screenWidth);
//            imageView.setLayoutParams(new GridView.LayoutParams(Constants.m_screenWidth,Constants.m_screenWidth));//设置ImageView宽高 
//            imageView.setAdjustViewBounds(false); 
//            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
//            imageView.setPadding(4, 4, 4, 4); 
//        } else { 
//            imageView = (ImageView) convertView; 
//        } 
//        imageView.setImageBitmap(listBitmap.get(position));
// 
//        return imageView; 
//    } 
//	private Integer[] mThumbIds = { 
//            R.drawable.liu, R.drawable.liu, 
//            R.drawable.liu, R.drawable.liu, 
//            R.drawable.liu, R.drawable.liu, 
//            R.drawable.liu, R.drawable.liu, 
//            R.drawable.liu, R.drawable.liu
//    };
	public void setListDataSetChanged(Bitmap bitmap) {
		if (bitmap != null)
			listBitmap.add(bitmap);
		System.out.println("size1:" + listBitmap.size());
		notifyDataSetChanged();

	}

	public int getCount() {
		// TODO Auto-generated method stub
		return listBitmap.size();
	}

	@Override
	public Object getItem(int index) {

		return listBitmap.get(index);
	}

	@Override
	public long getItemId(int index) {
		return index;
	}
	
	public void resletDataSetChanged(){
    	if(listBitmap!=null && listBitmap.size()>0)
    		for(Bitmap bitmap : listBitmap){
    			if(bitmap !=null && bitmap.isRecycled())
    				bitmap.recycle(); 
				bitmap=null;
    		}
    	listBitmap.clear();
    	listBitmap = null; 
    }
public static SoftReference<Bitmap> drawableToBitmap(Drawable drawable) {  
        
        Bitmap bitmap = Bitmap  
                        .createBitmap(  
                                        drawable.getIntrinsicWidth(),  
                                        drawable.getIntrinsicHeight(),  
                                        drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
                                                        : Bitmap.Config.RGB_565);  
        Canvas canvas = new Canvas(bitmap);  
        //canvas.setBitmap(bitmap);  
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());  
        drawable.draw(canvas);  
        return new SoftReference<Bitmap>(bitmap);  
    }  
    
 public static Bitmap drawableBitmap(Drawable drawable) {  
        
        Bitmap bitmap = Bitmap  
                        .createBitmap(  
                                        drawable.getIntrinsicWidth(),  
                                        drawable.getIntrinsicHeight(),  
                                        drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
                                                        : Bitmap.Config.RGB_565);  
        Canvas canvas = new Canvas(bitmap);  
        //canvas.setBitmap(bitmap);  
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());  
        drawable.draw(canvas);  
        return bitmap;  
    }  

}